(function($) {
	$.fn.fragLinks = function() {
		this.each(function() {
			$(this).attr('href', '#' + $(this).attr('href').substr(1));
		});
		return this;
	}
})(jQuery);

var navMarker = {
	marker : $('<div class="nav_highlight"/>'),
	prevLeft : 0,
	presentLeft : 0,
	slidLeft : function() {
		return (this.prevLeft < this.presentLeft);
	},
	slideTo : function(target) {
		this.prevLeft = this.presentLeft;
		this.presentLeft = target.position().left;
		this.marker.animate({
			'left' : this.presentLeft,
			'width' : target.outerWidth()
		}, 500, "easeInOutBack");
		$('#footer .nav').removeClass('current');
		target.addClass('current');
	}
}

var PANIM = {
	cache : {
	//	    '': $('.index')
	},
	newLeft: "-150%",
	oldLeft: "150%",
	showNewPage: function(content, url) {
		content.css({
			"display" : "block",
			"top" : 0,
			'left' : PANIM.newLeft
		}).show().animate({
			'left' : '50%'
		}, 700, "easeInOutQuart", function() {
			Cufon.refresh();
			if (url == "/content/directions.html") {
				window.location = "/directions.html";
			}
		});
	},
	hideOldPage: function(fragment) {
		$('.page_content:visible').animate({
			'left': PANIM.oldLeft
		}, 700, "easeInOutBack", function() {
				$(this).hide()
		});
		PANIM.getNewPage(fragment, PANIM.showNewPage);

	},
	pageChanged : function(e) {
			 
		navMarker.slideTo($('.nav a[href="#' + e.fragment + '"]'));
		if (navMarker.slidLeft()) {
			PANIM.oldLeft = '-150%';
			PANIM.newLeft = '150%';
		} else {
			PANIM.oldLeft = '150%';
			PANIM.newLeft = '-150%';
		}

		PANIM.getNewPage("/content/" + e.fragment, function() {});
		PANIM.hideOldPage("/content/" + e.fragment);

	},
	getNewPage : function(url, callback) {
		if (!PANIM.cache[url]) {
			PANIM.cache[url] = $('<div class="page_content"/>').appendTo(
					'#root_panel').hide()
			.load(url, function() {
				callback(PANIM.cache[url], url);
			});

		} 
		else {
			callback(PANIM.cache[url], url);
		}
		return PANIM.cache[url];
	}

}

$(function() {
	$('#footer .nav').append(navMarker.marker);
	$('#footer .nav a').fragLinks();
	DD_roundies.addRule('.nav_highlight', '15px');
	navMarker.slideTo($(".nav a[href='#" + $.url().attr('path').substr(1)+"']"));

	$(".nav").append(navMarker);
});

$(window).bind('hashchange', function(e) {
	PANIM.pageChanged(e);
});

/*    
 function handleHashChange(e) {
 doc_mode = doc.documentMode,
 supports_onhashchange = 'on' + str_hashchange in window && ( doc_mode === undefined || doc_mode > 7 );
 console.log(supports_onhashchange);
 var target = e.fragment;
 var url = '/content/'+target;
 console.log(url);

 var visible = $('.page_content:visible');
 var oldLeft;
 var newLeft;
 if (directionSwitch.isGoingLeft) {
 oldLeft = '-150%';
 newLeft = '150%';
 }
 else {
 oldLeft = '150%';
 newLeft = '-150%';
 }
 visible.animate({
 'left': oldLeft
 }, 500, function() {
 $(this).hide();
 });
 var currentLink = $( '.nav a[href="#' + url + '"]');
 var new_content;
 // if ( !cache[ url ] ) {
 // Show "loading" content while AJAX content loads.
 // $( '.loading' ).show();

 // Create container for this url's content and store a reference to
 // it in
 // the cache.
 new_content = $( '<div class="page_content"/>' )
 .appendTo('#root_panel')

 .load( url, function(){
 //		  new_content.append(response);
 // Content loaded, hide "loading" content.
 //	      $( '.loading' ).hide();
 });
 // new_content = cache[ url ];
 new_content.css({
 "top": 0,
 'left': newLeft
 }).show().animate({
 'left': '50%'
 }, 500, function() { Cufon.refresh();}
 );
 };
 // Remove class from any previously "current" link(s).

 // navMarker.slideTo(currentLink);

 */